<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 人
        function Person(name) {
            this.name = name
            // 构造函数在 new 的时候, 都发生了什么
            // 1. let obj = {}  虚拟的定义一个对象  
            // 2  this = obj    把 this 指针指向 obj
            // obj 的 __proto__ 原型指向构造函数的原型
            // 3. obj.__proto__ = Person.prototype
            // 4. return obj 把 obj 返回出去
        }
        Person.prototype.sleep = function() {
            this.name + ' 正在睡觉'
        }

        function inherit(subType, superType) {
            var TempFn =  function() {
                this.constructor = subType
            }
            TempFn.prototype = superType.prototype
            subType.prototype = new TempFn();
        }

        // 卡车司机
        function TruckDriver () {
            Person.apply(this, Array.from(arguments));
            this.card = 'A1'
        }
        inherit(TruckDriver, Person)

        TruckDriver.prototype.sleep = function() {
            console.log(this.name + '准备休息');
            console.log('1. 把车开到停车场')
            console.log('2. 找到旅馆')
            console.log('3. 休息')
        }

        // 学生
        function Student(status) {
            Person.apply(this, Array.from(arguments));
            this.techer = '张三'
            this.status = status
        }
        
        var TempFn = function() {
            this.constructor = Student
        }
        TempFn.prototype = Person.prototype
        Student.prototype = new TempFn();
        Student.prototype.sleep = function() {
            if (this.status === '在上课') {
                console.log('不能休息')
            } else {
                console.log('随意休息')
            }
        }

        let s = new Student('在上课')
        let s_1 = new Student('没有课')
        console.log(
            s
        )
        // s.sleep()
        // s_1.sleep()

        let t = new TruckDriver('卡车司机 A')
        // t.sleep()

        // 类型判断
        // function show(name) {
        //     if (typeof name === 'string') {
        //         alert(name)
        //     } else if (typeof name === 'number') {
        //         alert(name+1)
        //     }
        // }
        // show(99)

        // typeof       类名
        // instanceOf   原型检查
        // 构造函数名    s.__proto__.constructor.name

        // ------------------------------
        // // call   接受的参数是不定参
        // // apply

        // let o = {
        //     0: 1,
        //     1: 2,
        //     length: 2
        // }

        // Array.prototype.push.call(o, '000', '111', '222', '33', '55')
        // Array.prototype.push.apply(o, ['000', '111', '222', '33', '55'])
        // console.log(o);


        // bind

        let o = {
            name: '石兴龙'
        }

        function showName() {
            console.log(this.name, ' Hello', arguments);
        }
        Function.prototype.testBind = function() {
            let o = arguments[0]
            let args = [].slice.call(arguments, 1)
            console.log(o)
            console.log(args)
            let slef = this;

            return function() {
                return slef.apply(
                        o,
                        args.concat(Array.from(arguments))
                    )
            }
        }
        console.log('--------');
        let newF = showName.testBind(o, 1, 2, 3, 4)
        newF(5,6,7,8)

    </script>
</body>
</html>